import axios from 'axios'
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

export default new Vuex.Store({
  plugins: [createPersistedState()], //配置为 vuex 的插件 实现持久化
  state: {
    token:'',
    // token:localStorage.getItem('token') || '' 
    userInfo:{}, //存储用户信息
  },
  getters: {

  },
  mutations: {
    //更新token
    updataToken(state,token){
      state.token=token
      // localStorage.setItem('token',token)
    },
    //更新用户信息
    updateUserInfo(state,info){
      state.userInfo=info
    }
  },
  actions: {
    async initUserInfo(context){
      const {data:res} = await axios.get('/my/userinfo')
        context.commit('updateUserInfo',res.data)
    }
  },
  modules: {
  }
})
